package com.bacon.gmall.product.api;

import com.bacon.gmall.model.product.*;
import com.bacon.gmall.product.service.ProductService;
import com.bacon.gmall.product.utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;


/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2020-12-22 15:29
 * @description:
 */
@RestController
@RequestMapping("api/product")
public class ProductController {


    @Autowired
    private ProductService productService;


    /**
     * 查询1、2、3级
     */
    @GetMapping("/inner/getBaseCategoryView/{category3Id}")
    @Transactional(propagation = Propagation.NEVER)
    public BaseCategoryView getBaseCategoryView(@PathVariable Long category3Id) {
        return productService.getBaseCategoryView(category3Id);
    }

    /**
     * 查询SKUINFO 对象
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable Long skuId, HttpServletRequest request) throws SocketException, UnknownHostException {
        String ipAddr = IpUtil.getIpAddr(request);
        String localMac = IpUtil.getLocalMac(ipAddr);
        return productService.getSkuInfo1(skuId);
    }

    /**
     * 查询价格
     * getPrice
     */
    @GetMapping("/getPrice/{skuId}")
    public BigDecimal getPrice(@PathVariable Long skuId) {
        return productService.getPrice(skuId);
    }

    /**
     * 查询当前商品spu对应的所有销售属性及属性值集合 并skuid进行选中
     */
    @GetMapping("/inner/getSpuSaleAttrChecked/{spuId}/{skuId}")
    public List<SpuSaleAttr> getSpuSaleAttrChecked(@PathVariable Long spuId,
                                                   @PathVariable Long skuId) {
        return productService.getSpuSaleAttrChecked(spuId,skuId);
    }

    /**
     * 销售属性值ID的组合 对应 skuId数据
     */
    @GetMapping("/inner/getSaleAttrValuesBySpu/{spuId}")
    public Map getSaleAttrValuesBySpu(@PathVariable Long spuId){
        return productService.getSaleAttrValuesBySpu(spuId);
    }

    /**
     * 查询所有分类
     * @return  List<BaseCategoryView>
     */
    @GetMapping("/inner/getBaseCategoryViewList")
    public List<BaseCategoryView> getBaseCategoryViewList(){
        return productService.getBaseCategoryViewList();
    }

    @GetMapping("/inner/baseTrademark/{id}")
    public BaseTrademark getBaseTrademark(@PathVariable Long id) {
        return productService.getBaseTrademark(id);
    }

    @GetMapping("/inner/getBaseAttrInfoAndBaseAttrValue/{skuId}")
    public List<SkuAttrValue> getBaseAttrInfoAndBaseAttrValue(@PathVariable Long skuId){
        return productService.getBaseAttrInfoAndBaseAttrValue(skuId);
    }
}
